package com.zx.exp6.servlet;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zx.exp6.bean.Message;
@WebServlet(name="messageServlet", urlPatterns={"/messageServlet"})
public class MessageServlet extends HttpServlet{
	/*
	  <servlet>
	    <servlet-name>MessageServlet</servlet-name>
	    <servlet-class>com.zx.exp6.servlet.MessageServlet</servlet-class>
	  </servlet>
	  <servlet-mapping>
	    <servlet-name>MessageServlet</servlet-name>
	    <url-pattern>/messageServlet</url-pattern>
	  </servlet-mapping>
	 */
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		HttpSession session = req.getSession();
		String username = (String)session.getAttribute("username");
		String title = (String)req.getParameter("title");
		String content = (String)req.getParameter("content");
		Message message = new Message(username, title, content);
		ServletContext application = session.getServletContext();
		List<Message> messageList = (List<Message>)application.getAttribute("messageList");
		if(messageList == null){
			messageList = new LinkedList<Message>();
			application.setAttribute("messageList", messageList);
		}
		messageList.add(message);
		req.getRequestDispatcher("showMessage.jsp").forward(req, resp);
	}
	
}
